package demo;

import com.lm.mapper.clazzMapper;
import com.lm.mapper.companyMapper;
import com.lm.mapper.studentMapper;
import com.lm.pojo.Clazz;
import com.lm.pojo.Company;
import com.lm.pojo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Auther: Min.Liu
 * @Description:
 * @Date: Created in 14:30 2025/3/25
 */
public class MappingDemo {
    private SqlSession sqlSession;

    @Before
    public void before() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession(true);
    }

    @Test
    public void testManyToOne() {
        studentMapper mapper = sqlSession.getMapper(studentMapper.class);
        Student student = mapper.selectByid(1);
        System.out.println(student);
    }

    @Test
    public void testManyToOneLazy() {
        studentMapper mapper = sqlSession.getMapper(studentMapper.class);
        Student student = mapper.selectByidStep1(1);
        System.out.println(student.getSname());
        //验证懒加载
        System.out.println(student.getClazz().getCname());
    }

    @Test
    public void testOneToMany() {
        clazzMapper mapper = sqlSession.getMapper(clazzMapper.class);
        Clazz clazz = mapper.selectByid(1);
        System.out.println(clazz);
    }

    @Test
    public void testOneToManyLazy() {
        clazzMapper mapper = sqlSession.getMapper(clazzMapper.class);
//        Clazz clazz = mapper.selectByid2(1);
        List<Clazz> clazzes = mapper.selectByids(new int[]{1, 2});
        for (Clazz clazz : clazzes) {
            System.out.println(clazz);
        }

    }





}
